// jQuery File Tree Plugin
//
// Version 1.01
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 24 March 2008
//
// Visit http://abeautifulsite.net/notebook.php?article=58 for more information
//
// Usage: $('.fileTreeDemo').fileTree( options, callback )
//
// Options:  root           - root folder to display; default = /
//           script         - location of the serverside AJAX file to use; default = jqueryFileTree.php
//           folderEvent    - event to trigger expand/collapse; default = click
//           expandSpeed    - default = 500 (ms); use -1 for no animation
//           collapseSpeed  - default = 500 (ms); use -1 for no animation
//           expandEasing   - easing function to use on expand (optional)
//           collapseEasing - easing function to use on collapse (optional)
//           multiFolder    - whether or not to limit the browser to one subfolder at a time
//           loadMessage    - Message to display while initial tree loads (can be HTML)
//
// History:
//
// 1.01 - updated to work with foreign characters in directory/file names (12 April 2008)
// 1.00 - released (24 March 2008)
//
// TERMS OF USE
// 
// This plugin is dual-licensed under the GNU General Public License and the MIT License and
// is copyright 2008 A Beautiful Site, LLC.
//

var _timer = 0;
var t_ = true;

if (jQuery) (function ($) {

    $.extend($.fn, {
        fileTree: function (o, h) {
            // Defaults
            if (!o) var o = {};
            if (o.root == undefined) o.root = '/';
            if (o.script == undefined) o.script = 'jqueryFileTree.php';
            if (o.folderEvent == undefined) o.folderEvent = 'click';
            if (o.expandSpeed == undefined) o.expandSpeed = 500;
            if (o.collapseSpeed == undefined) o.collapseSpeed = 500;
            if (o.expandEasing == undefined) o.expandEasing = null;
            if (o.collapseEasing == undefined) o.collapseEasing = null;
            // if (o.multiFolder == undefined) 
            o.multiFolder = true;
            if (o.loadMessage == undefined) o.loadMessage = 'Loading...';
            var url = o.baseUrl;

            $(this).each(function () {

                function showTree(c, t) {

                    var url_ = unescape(t).replace(url, ""); //url.substring(0, url.length - 1);
                    $('.fe-newpath').val(url_.substring(0, url_.length - 1));
                    if (c && c.find("a")[0]) {
                        $('.fe-newpath1').val(c.find("a")[0].innerHTML);
                    }

                    $(c).addClass('wait');
                    $(".jqueryFileTree.start").remove();



                    var spaceType = $("#hiddenType_Name_IsAdmin").val();
                    //alert($("#hiddenType_Name_IsAdmin").val());
                    //                    if (spaceType) {
                    //                        spaceType = spaceType.split('|');
                    //                        if (spaceType[0] == "project") {
                    //                            spaceType = spaceType[0];
                    //                        }
                    //                    }


                    $.post(o.script, { dir: t, spaceType: spaceType, lefttree: '1' }, function (data) {

                        //                        if (spaceType == "project") {
                        if (t)
                            filemanager.load_contents(t.replace('/', ''), false);
                        //                        }
                        //                        else {
                        //                            // if (data.toString().indexOf("directory") < 0) {
                        //                            if(t)
                        //                            filemanager.load_contents("~/" + url_.substring(0, url_.length - 1), false);
                        //                        }


                        $(c).find('.start').html('');
                        $(c).removeClass('wait').append(data);

                        if (o.root == t) $(c).find('UL:hidden').show(); else $(c).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
                        bindTree(c);


                        $('#file_tree li a').click(function () {

                            $('#file_tree li a').css({ background: "white" });
                            $(this).css({ background: "#BDF" });
                        });


                    });
                }

                function displayContent(c, t) {

                    var url_ = unescape(t).replace(url, ""); //url.substring(0, url.length - 1);
                    
                    $('.fe-newpath').val(url_.substring(0, url_.length - 1));

                    if (c && c.find("a")[0]) {
                        $('.fe-newpath1').val(c.find("a")[0].innerHTML);
                    }
                    // $(c).addClass('wait');
                    $(".jqueryFileTree.start").remove();


                    var spaceType = $("#hiddenType_Name_IsAdmin").val();
                    //alert(spaceType);
                    //                    if (spaceType) {
                    //                        spaceType = spaceType.split('|');
                    //                        if (spaceType[0] == "project") {
                    //                            spaceType = spaceType[0];
                    //                        }
                    //                    }


                    $.post(o.script, { dir: t, spaceType: spaceType }, function (data) {

                        //                        if (spaceType == "project") {
                        if (t)
                            filemanager.load_contents(t.replace('/', ''), false);
                        //                        }
                        //                        else {
                        //                            // if (data.toString().indexOf("directory") < 0) {
                        //                            filemanager.load_contents("~/" + url_.substring(0, url_.length - 1), false);
                        //                        }


                        $(c).find('.start').html('');
                        // $(c).removeClass('wait');

                        // if (o.root == t) $(c).find('UL:hidden').show(); else $(c).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
                        //bindTree(c);


                        //  $('#file_tree li a').click(function () {

                        //      $('#file_tree li a').css({ background: "white" });
                        //       $(this).css({ background: "#BDF" });
                        //   });


                    });
                }



                function bindTree(t) {










                    $(t).find('LI A').bind('dblclick', function (e) {
                        t_ = false;
                        clearTimeout(_timer);

                        //  console.log('ondblclick');

                        if ($(this).parent().hasClass('directory')) {
                            if ($(this).parent().hasClass('collapsed')) {
                                // Expand
                                if (!o.multiFolder) {
                                    $(this).parent().parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
                                    $(this).parent().parent().find('LI.directory').removeClass('expanded').addClass('collapsed');
                                }
                                $(this).parent().find('UL').remove(); // cleanup
                                showTree($(this).parent(), escape($(this).attr('rel')));
                                $(this).parent().removeClass('collapsed').addClass('expanded');
                            } else {
                                // Collapse
                                $(this).parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
                                $(this).parent().removeClass('expanded').addClass('collapsed');
                            }
                        } else {
                            h($(this).attr('rel'));
                        }

                        setTimeout(function () {

                            t_ = true;
                        }, 301)
                        return false;

                    });
                    $(t).find('LI A').bind('click', function (e) {
                        var aObj = $(this);
                        _timer = setTimeout(function () {
                            if (t_) {
                                // console.log('onclick');

                                displayContent(aObj.parent(), escape(aObj.attr('rel')));
                                /*
                                if (aObj.parent().hasClass('directory')) {
                                if (aObj.parent().hasClass('collapsed')) {
                                // Expand
                                if (!o.multiFolder) {
                                aObj.parent().parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
                                aObj.parent().parent().find('LI.directory').removeClass('expanded').addClass('collapsed');
                                }
                                aObj.parent().find('UL').remove(); // cleanup
                                showTree(aObj.parent(), escape(aObj.attr('rel')));
                                aObj.parent().removeClass('collapsed').addClass('expanded');
                                } else {
                                // Collapse
                                aObj.parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
                                aObj.parent().removeClass('expanded').addClass('collapsed');
                                }
                                } else {
                                h(aObj.attr('rel'));
                                }
                                */


                                //showTree($(this).parent(), escape($(this).attr('rel')));


                            }


                        }, 300);

                    });


















                    /*


                    $(t).find('LI A').bind(o.folderEvent, function () {
                    if ($(this).parent().hasClass('directory')) {
                    if ($(this).parent().hasClass('collapsed')) {
                    // Expand
                    if (!o.multiFolder) {
                    $(this).parent().parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
                    $(this).parent().parent().find('LI.directory').removeClass('expanded').addClass('collapsed');
                    }
                    $(this).parent().find('UL').remove(); // cleanup
                    showTree($(this).parent(), escape($(this).attr('rel')));
                    $(this).parent().removeClass('collapsed').addClass('expanded');
                    } else {
                    // Collapse
                    $(this).parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
                    $(this).parent().removeClass('expanded').addClass('collapsed');
                    }
                    } else {
                    h($(this).attr('rel'));
                    }
                    return false;
                    });
                    */
                    // Prevent A from triggering the # on non-click events
                    if (o.folderEvent.toLowerCase != 'click') $(t).find('LI A').bind('click', function () { return false; });
                }
                // Loading message
                $(this).html('<ul class="jqueryFileTree start"><li class="wait">' + o.loadMessage + '<li></ul>');
                // Get the initial file list
                showTree($(this), escape(o.root));




            });
        }
    });

})(jQuery);


if (jQuery) (function ($) {

    $.extend($.fn, {
        fileTree1: function (o, h) {
            // Defaults
            if (!o) var o = {};
            if (o.root == undefined) o.root = '/';
            if (o.script == undefined) o.script = 'jqueryFileTree.php';
            if (o.folderEvent == undefined) o.folderEvent = 'click';
            if (o.expandSpeed == undefined) o.expandSpeed = 500;
            if (o.collapseSpeed == undefined) o.collapseSpeed = 500;
            if (o.expandEasing == undefined) o.expandEasing = null;
            if (o.collapseEasing == undefined) o.collapseEasing = null;
            //if (o.multiFolder == undefined) 
            o.multiFolder = true;
            if (o.loadMessage == undefined) o.loadMessage = 'Loading...';
            var url = o.baseUrl;
            var folderName = o.name;
            $(this).each(function () {
                
                function showTree(c, t) {
                    var url_ = unescape(t).replace(url, ""); //url.substring(0, url.length - 1);

                    $('.fe-newpath').val(url_.substring(0, url_.length - 1));
                    if (c && c.find("a")[0]) {
                        $('.fe-newpath1').val(c.find("a")[0].innerHTML);
                    }
                    $(c).addClass('wait');
                    $(".jqueryFileTree.start").remove();

                    var spaceType = $("#hiddenType_Name_IsAdmin").val();
                    //                    if (spaceType) {
                    //                        spaceType = spaceType.split('|');
                    //                        if (spaceType[0] == "project") {
                    //                            spaceType = spaceType[0];
                    //                        }
                    //                    }


                    $.post(o.script, { dir: t, spaceType: spaceType, lefttree: '0' }, function (data) {

                        // if (data.toString().indexOf("directory") < 0) {
                        //filemanager.load_contents("~/" + url_.substring(0, url_.length - 1), false);
                        // }

                        $(c).find('.start').html('');
                        $(c).removeClass('wait').append(data);
                        if (o.root == t) $(c).find('UL:hidden').show(); else $(c).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
                        bindTree(c);

                    });
                }

                function bindTree(t) {
                    $(t).find('LI A').bind(o.folderEvent, function () {
                        if ($(this).parent().hasClass('directory')) {
                            if ($(this).parent().hasClass('collapsed')) {
                                // Expand
                                if (!o.multiFolder) {
                                    $(this).parent().parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
                                    $(this).parent().parent().find('LI.directory').removeClass('expanded').addClass('collapsed');
                                }
                                $(this).parent().find('UL').remove(); // cleanup
                                showTree($(this).parent(), escape($(this).attr('rel')));
                                $(this).parent().removeClass('collapsed').addClass('expanded');
                            } else {
                                // Collapse
                                $(this).parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
                                $(this).parent().removeClass('expanded').addClass('collapsed');
                            }
                        } else {
                            h($(this).attr('rel'));
                        }
                        return false;
                    });
                    // Prevent A from triggering the # on non-click events
                    if (o.folderEvent.toLowerCase != 'click') $(t).find('LI A').bind('click', function () { return false; });
                }
                // Loading message
                $(this).html('<ul class="jqueryFileTree start"><li class="wait">' + o.loadMessage + '<li></ul>');
                // Get the initial file list
                showTree($(this), escape(o.root));
            });
        }
    });

})(jQuery);